package com.neteast.androidclient.contact.activity;

import java.util.ArrayList;
import com.neteast.androidclient.contact.R;
import com.neteast.androidclient.contact.adapter.ContactPageAdapter;
import com.neteast.androidclient.contact.adapter.SingleScreenAdapter;
import com.neteast.androidclient.contact.adapter.SearchResultAdapter;
import com.neteast.androidclient.contact.domain.Contact;
import com.neteast.androidclient.contact.util.NetUtil;

import android.app.Activity;
import android.app.Dialog;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v4.view.ViewPager;
import android.util.Log;
import android.view.Display;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.WindowManager.LayoutParams;
import android.view.WindowManager;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.GridView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

public class SearchActivity extends Activity implements OnClickListener {
	private final String TAG = "SearchActivity";
	private final int SEARCH_FINISH = 1;
	private LinearLayout noResultLayout;
	private Button btnBack;
	private EditText searchInput;
	private ViewPager resultGrid;
	private TextView noResultHint;
	private Button btnSearch;
	/** 判断搜索范围 ，为真时搜索云好友， 否则搜索通讯好友 */
	private boolean isCloud;
	private Context mContext;
	private int currentView;
	private Dialog progressDialog;
	private ContactActivity contactActivity;
	public OnItemClickListener gridListener = new OnItemClickListener() {

		public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
				long arg3) {
			// TODO Auto-generated method stub
			
		}};
	private Handler handler = new Handler(){

		@Override
		public void handleMessage(Message msg) {
			switch(msg.what){
			case SEARCH_FINISH:
				final ArrayList<Contact> result = (ArrayList<Contact>) msg.obj;
				if(isCloud){
					currentView = 1;
				}else{
					currentView = 2;
				}
				ArrayList<View> pageListView ;
				ContactPageAdapter pageAdapter;
				if(result != null && result.size() != 0){
					pageListView = setGrid(result);
					pageAdapter = new ContactPageAdapter(pageListView); 
					resultGrid.setAdapter(pageAdapter);
					resultGrid.setVisibility(View.VISIBLE);
					noResultLayout.setVisibility(View.GONE);
				}else{
					resultGrid.setVisibility(View.GONE);
					noResultLayout.setVisibility(View.VISIBLE);
					noResultHint.setText(getString(R.string.search_no_result_1,
							searchInput.getText().toString().trim()));
				}
				progressDialog.dismiss();
				break;
			}
			
			super.handleMessage(msg);
		}};
	

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);

		setContentView(R.layout.search);
		findView();
		init();
	}

	private void findView() {
		noResultLayout = (LinearLayout) findViewById(R.id.noResultLayout);
		btnBack = (Button) findViewById(R.id.btnBack);
		searchInput = (EditText) findViewById(R.id.fuuzzyEdtInput);
		resultGrid = (ViewPager) findViewById(R.id.resultGrid);
		noResultHint = (TextView) findViewById(R.id.noResultHint);
		btnSearch = (Button) findViewById(R.id.btnSmallSearch);
		btnBack.setOnClickListener(this);
		btnSearch.setOnClickListener(this);
	}

	private void init() {
		WindowManager wManager = getWindowManager();
		Display display = wManager.getDefaultDisplay();
		LayoutParams lParams = getWindow().getAttributes();
		lParams.height = display.getHeight() - 105;
		lParams.width = display.getWidth();
		getWindow().setAttributes(lParams);
		getWindow().setGravity(Gravity.BOTTOM);
		mContext = SearchActivity.this;
		isCloud = getIntent().getBooleanExtra("isCloud", false);
		contactActivity = new ContactActivity();
		progressDialog = new Dialog(mContext, R.style.progressDialogStyle);
	}

	/**
	 * 将联系人列表数据按照每屏固定数量分屏加入Viewpager的列表
	 * @param list 联系人列表数据
	 * */
	public ArrayList<View> setGrid(ArrayList<Contact> list) {
		ArrayList<View> viewList = new ArrayList<View>();
		int PageCount = (int) Math.ceil(list.size() / contactActivity.PAGE_SIZE);
		GridView singleScreen;
		for (int i = 0; i < PageCount; i++) {
			singleScreen = (GridView) LayoutInflater.from(this).inflate(
					R.layout.single_screen, null);
			singleScreen.setAdapter(new SingleScreenAdapter(mContext, list, i, 
					handler, currentView, progressDialog));
			singleScreen.setOnItemClickListener(gridListener);
			viewList.add(singleScreen);
		}
		return viewList;
	}

	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.btnBack:
			finish();
			break;
		case R.id.btnSmallSearch:
			final String searchKey = searchInput.getText().toString().trim();
			if (!searchKey.equals("")) {
				contactActivity.showProgressDialog(mContext, progressDialog, "正在搜索...");
				if(isCloud){
					new Thread(new Runnable() {
						public void run() {
							ArrayList<Contact> result = NetUtil.searchContacts(1, searchKey);
							handler.obtainMessage(SEARCH_FINISH, result).sendToTarget();
						}
					}).start();
				}else{
					new Thread(new Runnable() {
						public void run() {
							ArrayList<Contact> result = NetUtil.searchContacts(0, searchKey);
							handler.obtainMessage(SEARCH_FINISH, result).sendToTarget();
						}
					}).start();
				}
				
				
			} else {
				Toast.makeText(mContext, "请输入模糊搜索关键字", Toast.LENGTH_SHORT)
						.show();
			}
			break;

		}

	}
}
